import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as ReactRouter from 'react-router';
import * as Reflux from 'reflux';

import { AddActions,AddStore } from './action';
import SearchInput from './search';
import { SUB } from './sub';
import Page from '../list/page';
import Table from '../list/table';

var Link = ReactRouter.Link;
var IN = SearchInput(AddActions, AddStore).IN;

var DoSth = React.createClass({
    render: function () {
        var actionAuths = this.props.param.actionAuths, $show = [], purchaseOrderStepName;
        if (actionAuths.isEdit) {
            $show.push(<Link to={`/demand/create/${this.props.param.id}`} className="btn btn-icon icon-edi" title="编辑" key={1}>编辑</Link>);
        }
        if (actionAuths.isViewDetail) {
            $show.push(<Link to={`/demand/detail/${this.props.param.id}`} className="btn btn-icon icon-edi" title="查看" key={2}>查看</Link>);
        }
         //区域经理审核
        if (actionAuths.isReview&&this.props.param.demandStep=="2") {
            $show.push(<Link to={`/demand/check/${this.props.param.id}`} className="btn btn-icon icon-edi" title="审核" key={3}>审核</Link>);
        }
        //采购审核
        if (actionAuths.isReview&&this.props.param.demandStep=="3") {
            $show.push(<Link to={`/demand/audit/${this.props.param.id}`} className="btn btn-icon icon-edi" title="审核" key={4}>审核</Link>);
        }

        if (actionAuths.isCreate) {
            $show.push(<Link to={`/demand/supplement/${this.props.param.id}`} className="btn btn-icon icon-edi" title="填写采购增补单" key={5}>填写采购增补单</Link>);
        }

        return (
            <label className="inline-f">
                {$show}
            </label>
        )
    }
});

var List = React.createClass({
    mixins: [Reflux.connect(AddStore, 'dataResult')],
    componentWillMount: function () {
        AddActions.getList();
    },
    handleSubmit: function () {
        AddActions.getList();
    },
    render: function () {
        var result = this.state.dataResult &&this.state.dataResult.result.items||[];
        var page = this.state.dataResult ? {
            currentPage: this.state.dataResult.result.currentPage,
            total: this.state.dataResult.result.total,
            totalPages: this.state.dataResult.result.totalPages
        } : false;
        return (
            <div className="temp-column">
                <div className="container-search-info">
                    <div className="search-conditions">
                        {(() => {
                            var inputs = [];
                            for (inputParam in this.props.searchParam) {
                                inputs.push(
                                    <div className="form-group">
                                        <IN
                                            param={this.props.searchParam[inputParam]}
                                            actions={AddActions}
                                            store={AddStore}
                                            onKeyUp={this.handleSubmit}
                                        />
                                    </div>
                                );
                            }
                            return inputs;
                        })()}
                        <div className="form-group">
                            <SUB handleSubmit={this.handleSubmit} title={"查询"} />
                        </div>
                    </div>
                </div>
                <div className="container-table">
                    {(() => {
                        if(this.state.dataResult){
                            return(
                                <Table list={result} param={this.props.searchParam} DoSth={this.props.DoSth} />
                            );
                        }else{
                            return (
                                <div className>loading...</div>
                            );
                        }
                     })()}
                    <Page page={page} actions={AddActions} store={AddStore} dd="dddd"/>
                </div>
            </div>
        )
    }
});

module.exports = { List };
